package com.youbug.mall.coupon_sms.config;

import cn.hutool.crypto.symmetric.AES;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.youbug.common.config.MyDataSource;
import com.youbug.common.config.MyMetaObjectHandler;
import io.seata.rm.datasource.DataSourceProxy;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@Slf4j
@MapperScan(basePackages = "com.youbug.mall.coupon_sms.mapper", sqlSessionTemplateRef = "sqlSessionTemplateSms")
public class DataSourceSmsConfig {

    @Autowired
    MybatisPlusInterceptor mybatisPlusInterceptor;
    @Autowired
    MyMetaObjectHandler myMetaObjectHandler;

    @Bean(name = "sms")
    @ConfigurationProperties(prefix = "spring.datasource.sms")
    public DataSource dataSourcePms(AES aes) {
        return new MyDataSource(aes);
    }
    @Bean(name = "smsPxy")
    public DataSource dataSource(@Qualifier("sms") DataSource dataSource) {

        return new DataSourceProxy(dataSource);
    }

    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/youbug/mall/coupon_sms/mapper/xml/*.xml"));
        bean.setGlobalConfig(globalConfig());
        bean.setPlugins(mybatisPlusInterceptor);

        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        // 配置打印sql语句
        if(log.isDebugEnabled()){
            configuration.setLogImpl(StdOutImpl.class);
        }
        bean.setConfiguration(configuration);
        return bean.getObject();
    }

    @Bean(name = "transactionManagerSms")
    public DataSourceTransactionManager transactionManager(@Qualifier("smsPxy") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionTemplateSms")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("smsPxy") DataSource dataSource) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory(dataSource));
    }

    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(myMetaObjectHandler);
        return globalConfig;
    }
}
